///=============内部商城管理


//查看图片对话框
function clickRow(evt) {
    var type = 0;
    console.log(evt);

    if (evt.data.action == "查看图片") {
        var row = evt.data.row;
        openCustModal(type, row);
    }
}

//上传图片对话框
function uploadCustModal() {
    var type = 1;

    if (h3ui.widgets.FormList3.props.selectedRowKeys.length != 1) {
        h3ui.$message.info("请选择一行数据");
        return false;
    }

    var selectRow = h3ui.widgets.FormList3.props.selectedRow[0];

    openCustModal(type, selectRow);
}

// 打开对话框
function openCustModal(type, row) {

    var submitId = "Buttons18";
    var modalId = "CustomModal1";
    var fields = ["images", "id"];
    var editIds = ["FormModelItem6"];
    //领域模型值设置为空
    h3ui.models.t_goods_purchase.newItem();

    //给指定领域模型值赋值
    fields.forEach(a => {
        var val = row[a];

        if (a == "images" && val) {
            val.forEach(b => {
                b.uid = b.id;
                b.status = "done";
                b.url = "/api/file/download?refId=" + b.id;
            })
        }
        h3ui.models.t_goods_purchase.item[a] = val;
    });


    var display, editable, title;

    if (type == 1) {
        display = "";
        editable = true;
        title = "上传图片";
    } else {
        display = "none";
        editable = false;
        title = "图片预览";
    }

    //提交按钮隐藏
    h3ui.widgets[submitId].props.visible.display = display;
    //是否可以编辑
    // h3ui.widgets.FormModel1.props.editable =editable;
    editIds.forEach(a => h3ui.widgets[a].props.editable = editable);

    h3ui.widgets.CustomModal1.props.title = title;

    h3ui.widgets[modalId].props.visible = true;
}


//提交
function submitModal(evt) {
    var dataSource = h3ui.models.t_goods_purchase;
    var convertData = {
        id: dataSource.item.id,
        images: dataSource.item.images
    }
    dataSource.convert(convertData, dataSource.schema);

    var data = {
        id: convertData.id,
        images: convertData.images
    }

    h3ui.services.modifyPurchaseImages({data}).then(res => {
        h3ui.$message.success('提交成功');

        h3ui.widgets["CustomModal1"].props.visible = false;

        doReloadList({node: {id: "FormList3"}});


    }).catch(res => {
        h3ui.$message.error(res.errMessage || '提交失败');
    })


}


//列表刷新
function doReloadList(evt) {
    var list = h3ui.widgets[evt.node.id];


    var filter = h3ui.widgets[list.childIds[0]].props.value;

    var pageIndex = evt.pageIndex;
    var pageSize = 10;
    if (!pageIndex) {
        pageIndex = h3ui.widgets[list.childIds[3]].props.current;
    }

    list.emit("load", {pageIndex, pageSize, filter});

    list.props.selectedRowKeys = [];
}


/**
 * 打开修改目录对话框
 */

function openCatalogModel() {


    if (h3ui.widgets.FormList3.props.selectedRowKeys.length != 1) {
        h3ui.$message.info("请选择一行数据");
        return false;
    }

    var row = h3ui.widgets.FormList3.props.selectedRow[0];

    var dataSource = h3ui.models.t_goods_purchase;

    dataSource.newItem();

    dataSource.item.id = row.id;
    dataSource.item.catalog = row.catalog;

    if (row.catalog_id) {
        dataSource.item.catalog_id = [{
            id: row.catalog_id,
            name: row.catalog
        }];
    }

    h3ui.widgets.CustomModal2.props.visible = true;

}

/**
 *  提交目录修改
 * @returns
 */
function submitCatalog() {
    console.log("提交目录修改")
    var dataSource = h3ui.models.t_goods_purchase;

    var {catalog, catalog_id, id} = dataSource.item;

    if (catalog_id == null || catalog_id.length != 1) {
        h3ui.$message.info("请选择目录");
        return false;
    }

    catalog = catalog_id[0].name;
    catalog_id = catalog_id[0].id;

    var data = {catalog, catalog_id, id};

    h3ui.services.modifyPurchaseCatalog({data}).then(res => {
        h3ui.$message.success('提交成功');

        h3ui.widgets["CustomModal2"].props.visible = false;

        doReloadList({node: {id: "FormList3"}});


    }).catch(res => {
        h3ui.$message.error(res.errMessage || '提交失败');
    })


}